package drds.global_transaction.api.rpc.config;

import drds.global_transaction.config.Configuration;
import drds.global_transaction.config.FileConfiguration;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.epoll.Epoll;
import lombok.Getter;
import lombok.Setter;

public class ServerConfig extends Config
{

	/**
	 * The constant DIRECT_BYTE_BUF_ALLOCATOR.
	 */
	public static final PooledByteBufAllocator DIRECT_BYTE_BUF_ALLOCATOR = new PooledByteBufAllocator(true);//, worker_thread_size, worker_thread_size, 2048 * 64, 10, 512, 256, 64, true, 0);
	private static final int default_listen_port = 8091;
	private static final int rpc_request_timeout = 30 * 1000 * 100000;
	private static final Configuration CONFIG = new FileConfiguration();
	private static final String default_boss_thread_prefix = "NettyBoss";
	private static final String epoll_worker_thread_prefix = "NettyServerEPollWorker";
	private static final String nio_worker_thread_prefix = "NettyServerNIOWorker";
	private static final String default_executor_thread_prefix = "NettyServerBizHandler";
	private static final int default_boss_thread_size = 1;

	public static boolean enableEpoll()
	{
		return Epoll.isAvailable();

	}

	public static int getRpcRequestTimeout()
	{
		return rpc_request_timeout;
	}

	@Setter
	@Getter
	private int serverSelectorThreads = worker_thread_size;
	@Setter
	@Getter
	private int serverSocketSendBufSize = 153600;
	@Setter
	@Getter
	private int serverSocketResvBufSize = 153600;
	@Setter
	@Getter
	private int serverWorkerThreads = worker_thread_size;
	@Setter
	@Getter
	private int soBackLogSize = 1024;
	@Setter
	@Getter
	private int writeBufferHighWaterMark = 67108864;
	@Setter
	@Getter
	private int writeBufferLowWaterMark = 1048576;

	@Setter
	@Getter
	private boolean enableServerPooledByteBufAllocator = true;

	@Setter
	@Getter
	private int serverChannelMaxIdleTimeSeconds = 30;

	public String getBossThreadPrefix()
	{
		return CONFIG.getConfig("transport.thread-factory.boss-thread-prefix", default_boss_thread_prefix);
	}

	public int getBossThreadSize()
	{
		return CONFIG.getInt("transport.thread-factory.boss-thread-size", default_boss_thread_size);
	}

	public int getChannelMaxReadIdleSeconds()
	{
		return max_read_idle_seconds;
	}

	public int getDefaultListenPort()
	{
		return default_listen_port;
	}

	public String getExecutorThreadPrefix()
	{
		return CONFIG.getConfig("transport.thread-factory.transaction-coordinator-executor-thread-prefix", default_executor_thread_prefix);
	}

	public String getWorkerThreadPrefix()
	{
		return CONFIG.getConfig("transport.thread-factory.worker-thread-prefix", enableEpoll() ? epoll_worker_thread_prefix : nio_worker_thread_prefix);
	}

}
